<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="typeId">
        <el-input
            v-model="queryParams.typeId"
            placeholder="请输入ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验" prop="typeName">
        <el-input
            v-model="queryParams.typeName"
            placeholder="请输入实验"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            :disabled="multiple || !canDo"
            @click="toFinishs"
        >完成实验
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            :disabled="multiple"
            @click="toTops"
        >置顶
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs
        v-model="activeTab"

        type="card"
        class="demo-tabs"
        @tab-change="handleChange"
    >
      <el-tab-pane
          v-for="item in tabs"
          :name="item.name"
          :label="item.label"
      >
      </el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="experimentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column align="center" width="30">
        <template #default="scope">
          <el-tooltip
              class="box-item"
              effect="dark"
              :content="scope.row.topReason"
              placement="top-start"
          >
            <el-icon v-if="scope.row.orderNum==0" style="color: red"><Upload /></el-icon>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="experimentDate"/>
      <el-table-column label="项目名称" prop="projectName"/>
      <el-table-column label="图片" align="center" prop="sampleImage" width="120">
        <template #default="scope">
          <el-image
              v-if="scope.row.sampleImage"
              style="width: 100px; height: 100px"
              :src="scope.row.sampleImage.split(';')[0]"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="scope.row.sampleImage.slice(0,-1).split(';')"
              :initial-index="4"
              fit="cover"
              preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="样品编号" prop="sampleName"/>
      <el-table-column label="实验状态" prop="expermentType">
        <template #default="scope">
          <div v-if="scope.row.experimentEndDate!=null" style="background-color: #01be04;color: white;border-radius: 30%;font-size: 12px;padding: 3px 5px;display: inline-block">
            已完成
          </div>
          <div v-else style="background-color: #f51236;color: white;border-radius: 30%;font-size: 12px;padding: 3px 5px;display: inline-block">
            未完成
          </div>
        </template>

      </el-table-column>
      <el-table-column label="实验要求" prop="experimentRemark"/>
      <el-table-column label="实验操作人" prop="experimentPerson"/>
      <el-table-column label="实验要求完成时间" prop="experimentRequiredDate" width="150px"/>
      <el-table-column label="实验完成时间" prop="experimentEndDate" width="120px"/>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <div v-if="scope.row.confirmStatus==0">
            <el-button link type="primary" v-if="scope.row.experimentEndDate==null"  @click="toFinish(scope.row.experimentId)">完成</el-button>
          </div>
          <div v-if="scope.row.confirmStatus==16">
            <el-button link type="success" icon="Edit" @click="handleConfirm(scope.row.sampleId,scope.row.confirmStatus)">确认</el-button>
          </div>
          <el-button link type="primary" v-if="scope.row.orderNum==0" @click="toCancel(scope.row.experimentId)">取消置顶</el-button>
          <el-button link type="primary" v-else  @click="toTop(scope.row.experimentId)">置顶</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <el-dialog title="实验完成" v-model="open" width="400">
      <el-form :model="form" ref="finishRef" :rules="rules">
        <el-form-item label="实验操作人" prop="experimentPerson">
          <el-input v-model="form.experimentPerson" placeholder="请填写实验操作人" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="操作时间" prop="experimentEndDate">
          <el-date-picker
              v-model="form.experimentEndDate"
              type="datetime"
              placeholder="请选择操作时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="finish">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="置顶" v-model="openTop" width="300">
      <el-form>
        <el-form-item label="置顶原因">
          <el-input v-model="reason" placeholder="请填写置顶原因"/>
        </el-form-item>
        <el-form-item style="align-items: center">
          <el-button type="primary" @click="submitTop" style="margin-left: 35%;">置顶</el-button>
        </el-form-item>
      </el-form>


    </el-dialog>
  </div>
</template>

<script setup>
import {cancelTopExperiment, finishExperiment, selectExceptions, selectTypes, topExperiment} from "@/api/post/post.js";
import {useRoute} from "vue-router";
import {ElMessage, ElNotification} from "element-plus";
import {Cconfirm} from "@/api/sample/sample.js";

const {proxy} = getCurrentInstance();

const experimentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const finishRef=ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    experimentPerson: [
      {required: true, message: '请填写实验名称', trigger: 'blur'}
    ],
    experimentEndDate: [
      {required: true, message: '请选择实验完成日期', trigger: 'blur'}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

const activeTab=ref(0);
const tabs=ref([]);

const route=useRoute();
const jobId=route.query.jobId;
queryParams.value.jobId=jobId;
/** 查询实验类型管理列表 */
function getList() {
  loading.value = true;
  selectExceptions(queryParams.value).then(response => {
    experimentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleChange(jobId){
  queryParams.value.typeId=jobId;
  getList();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    typeId: null,
    typeName: null,
    describe: null
  };
  proxy.resetForm("experimentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.jobId=jobId;
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize=10;
  activeTab.value='';
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.experimentId);
  multiple.value = !selection.length;
  canDo.value=true;
  for (let e of selection) {
    if (e.confirmStatus!=0){
      canDo.value=false;
      break;
    }
  }
}
function toFinish(id){
  open.value=true;
  form.value={};
  form.value.ids=[id];
}
function toFinishs(){
  open.value=true;
  form.value={};
  form.value.ids=ids.value;
}
function finish(){
  proxy.$refs["finishRef"].validate(valid => {
    if (valid){
      finishExperiment(form.value).then(res=>{
        if (res.code == 208) {
          ElMessage({message: res.data.msg, type: 'error'})
          return;
        }
        open.value=false;
        getList();
      })
    }
  })
}
//置顶
const reason=ref();
const openTop=ref(false);

function toTop(id){
  ids.value=[id];
  reason.value="";
  openTop.value=true;
}
function toTops(){
  if (ids.value.length==0){
    ElNotification({message:'请至少选择一条信息',type:'warning',title:'提示'});
    return;
  }
  reason.value="";
  openTop.value=true;
}

function submitTop(){
  topExperiment({ids:ids.value,reason:reason.value}).then(res=>{
    getList();
    openTop.value=false;
  })
}
function toCancel(id){
  let ids=[id];
  cancelTopExperiment(ids).then(res=>{
    getList();
  })
}
selectTypes(jobId).then(res=>{
  for (let tab of res.data) {
    tabs.value.push({name:tab.typeId,label:tab.typeName});
  }
  if(tabs.value.length>0){
    activeTab.value=tabs.value[0].typeId
    queryParams.value.typeId=activeTab.value
  }
  getList();
})
//确认状态
const canDo=ref(true);

function handleConfirm(id,status){
  Cconfirm(id).then(res=>{
    if (res.data){
      ElNotification({title:'提示',type:'success',message:'确认成功'})
      getList();
    }
  })
}

</script>
<style scoped>

</style>
